varappServer = 'http://localhost:3000';
varbucket = 'js-sdk-bucket-sts';
varregion = 'oss-cn-hangzhou';

//varurllib = OSS.urllib;
var ossClient={};
var applyTokenDo = function (file, funSuccess,failed) {
    $.ajax({
        headers : {
            'Accept' : 'application/json',
            'Content-Type' : 'application/json'
        },
        type : "GET",
        url :	"http://47.114.219.249:88/abkj-api-auth-provider/login/stsToken",
        success : function(message) {
            console.log(message);
            var creds = message.data;
            if (message.code == 0) {
                ossClient=new OSS({
                    region: creds.region,
                    accessKeyId: creds.AccessKeyId,
                    accessKeySecret: creds.AccessKeySecret,
                    stsToken: creds.SecurityToken,
                    bucket: creds.bucket
                });
                uploadFile(file, ossClient,creds,funSuccess);
            } else {
                alert("oss token error")
            }
        }
    });
};

var currentCheckpoint;
var progress =  function(p, checkpoint) {
    currentCheckpoint = checkpoint;
    var bar = document.getElementById('progress-bar');
    if (bar) {
	    bar.style.width = Math.floor(p * 100);
	    bar.innerHTML = Math.floor(p * 100);
    }
};

var uploadFile = function (file, client,creds,success) {
    var file = file || document.getElementById('file').files[0];
    var nameStr = file.name;
    var index = nameStr.lastIndexOf(".");
    var typeStr=""
    if(index != -1){
       typeStr = nameStr.substring(index).toLowerCase();
    }

    // var key = document.getElementById('object-key-file').value.trim() || 'object';
    // console.log(file.name + ' => ' + key);
    var ossFileName = creds.fileName+typeStr;
    var ret = client.multipartUpload(ossFileName, file, {
        progress: progress
    }).then(function (res) {
        success(res) ;
        console.log('upload success: %j', res);
        //return listFiles(client);
    })
    
};

// window.onload = function () {
//     document.getElementById('file-button').onclick = function () {
//         applyTokenDo(uploadFile);
//     }
// };
